/*   The MIT License
 *   
 *   Carina Engine
 *   Copyright (c) 2013 Zdravko Velinov
 *   
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef _CARINA_CONFIG_AST_HH_
#define _CARINA_CONFIG_AST_HH_

#include "carina/ast.hh"

namespace Carina
{
namespace Config
{
class Entry;
class VisitorInterface;

enum NodeType
{
    CE_CONFIG_ENTRY = AST::CE_AST_NODES,
    CE_CONFIG_NODES
};
}

namespace AST
{
using namespace Carina::Config;
CE_AST_NODE_INFO(Entry, CE_CONFIG_ENTRY, Config::VisitorInterface)
}

namespace Config
{
using namespace AST;

class Entry
{
    string        m_Name;
    AST::Node     m_Value;
public:
    Entry(string name, AST::Node value);
     ~Entry();

    Entry(Entry&& val)
        :   m_Name(std::move(val.m_Name)),
			m_Value(std::move(val.m_Value)) {}
    Entry& operator=(Entry&& val) { m_Name = std::move(val.m_Name); m_Value = std::move(val.m_Value); return *this; }
    
    AST::Node* getValue();
    const AST::Node* getValue() const;

    string getNodeName() const { return m_Name; }
    
    bool isBlockStatement() const;
private:
    Entry(const Entry&);
    Entry& operator=(const Entry&);
};

class VisitorInterface: public AST::VisitorInterface
{
public:
    virtual void visit(const Entry* _entry)=0;
};

void PrintNode(Config::VisitorInterface* visitor, AST::PrinterInfrastructure* printer, const Entry* _entry);

class Printer: public Config::VisitorInterface
{
    AST::PrinterInfrastructure m_Printer;
public:
    Printer(std::ostream& os, size_t flags);
    virtual ~Printer();
    
    virtual void visit(const Value<float>* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const Value<int>* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const Value<unsigned>* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const Value<bool>* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const Value<string>* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const ListElement* lst) override { PrintNode(this, &m_Printer, lst); }
    virtual void visit(const Block* _block) override { PrintNode(this, &m_Printer, _block);}
    virtual void visit(const StringLiteral* value) override { PrintNode(&m_Printer, value); }
    virtual void visit(const Entry* _entry) override { PrintNode(this, &m_Printer, _entry); }
};
}
}

#endif // _CARINA_CONFIG_AST_HH_